//
//  HNCreditGoldSelectView.m
//  XDEducation
//
//  Created by 星道 on 17/5/27.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "HNCreditGoldSelectView.h"

@implementation HNCreditGoldSelectView
{
    UIView *_View;
    UIScrollView *_Title_View;
    NSString *_RowTitle;
    BOOL _Select;
    void(^_SelectRow)(NSString *);
}

- (NSArray *)title_Arr {
    
    if (_title_Arr == nil) {
        self.title_Arr = [NSArray array];
    }
    return _title_Arr;
}
- (instancetype)initWithTitle_Arr:(NSArray *)Title_Arr SelectRow:(void (^)(NSString *))SelectRow
{
    self = [super init];
    if (self)
    {
        self.alpha = 0;
        
        self.backgroundColor = UIColorWithRGBA(0, 0, 0, 0.65);
        self.frame = CGRectMake(0, 0, ScreenW, ScreenH);
        
        [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(Close)]];
        
        self.title_Arr = Title_Arr;
        _SelectRow = SelectRow;
        
        //UI
        [self UIWithTitle_Arr];
        
        //打开
        [self Open];
    }
    return self;
}







- (void)Close
{
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        
        if (_SelectRow && _Select)
        {
            _SelectRow (_RowTitle);
        }
        [self removeFromSuperview];
    }];
}

- (void)Open
{
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 1;
    }];
}





#pragma mark - UI
- (void)UIWithTitle_Arr
{
    [self addSubview:[self MainViewWithIndex:self.title_Arr.count]];
    
    [self SetTitleButton];
    
}



#pragma mark - UI.Frame
- (UIView *)MainViewWithIndex:(NSInteger)Index
{
    CGFloat X = 0;
    CGFloat Y = 0;
    CGFloat W = self.frame.size.width*0.8;
    CGFloat H = 45 + (40*Index+1);
    if (H > self.frame.size.height-40)
    {
        H = self.frame.size.height- 40;
    }
    UIView *View = [[UIView alloc] initWithFrame:CGRectMake(X, Y, W, H)];
    View.backgroundColor = [UIColor whiteColor];
    View.center = CGPointMake(ScreenW/2, ScreenH/2);
    _View = View;
    
    X = 8;
    Y = 0;
    W = View.frame.size.width-16;
    H = 45;
    UILabel *Title_label = [[UILabel alloc] initWithFrame:CGRectMake(X, Y, W, H)];
    Title_label.text = @"请选择";
    Title_label.textColor = ThemeColor;
    [View addSubview:Title_label];
    
    X = 0;
    Y = Title_label.frame.size.height;
    W = View.frame.size.width;
    H = 2;
    UIView *Line_view = [[UIView alloc] initWithFrame:CGRectMake(X, Y, W, H)];
    Line_view.backgroundColor = ThemeColor;
    [View addSubview:Line_view];
    
    X = 0;
    Y = Line_view.frame.origin.y+Line_view.frame.size.height;
    W = View.frame.size.width;
    H = View.frame.size.height-Y;
    UIScrollView *Title_View = [[UIScrollView alloc] initWithFrame:CGRectMake(X, Y, W, H)];
    Title_View.contentSize = CGSizeMake(W, 40*Index);
    _Title_View = Title_View;
    [View addSubview:Title_View];
    
    return View;

}


- (void)SetTitleButton
{
    for (int i=0; i<self.title_Arr.count; i++)
    {
        CGFloat x = 8;
        CGFloat y = i*40.0;
        CGFloat w = _View.frame.size.width-16;
        CGFloat h = 40;
        
        UIButton *Button = [UIButton buttonWithType:(UIButtonTypeCustom)];
        Button.frame = CGRectMake(x, y, w, h);
        [Button setTitle:self.title_Arr[i] forState:(UIControlStateNormal)];
        Button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        Button.tag = 1100+i;
//        Button.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:17];
        [Button setTitleColor:UIColorFromRGB(0x4c4c4c) forState:(UIControlStateNormal)];
        [Button addTarget:self action:@selector(ClickButton:) forControlEvents:(UIControlEventTouchUpInside)];
        [_Title_View addSubview:Button];
    }
}

- (void)ClickButton:(UIButton *)Button
{
    _Select = YES;
    _RowTitle = Button.titleLabel.text;
    [self Close];
}
@end
